package com.hulushuju.core.net.cookie;

import com.hulushuju.core.net.exception.HttpException;
import com.hulushuju.core.net.exception.InstanceFailedException;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.HttpCookie;
import java.util.List;

/**
 * @Author: Hanjiafeng
 * @Date: 2019-05-29
 * @Desc: Cookie管理器, 用于获取指定实现的Cookie管理策略, 设置和获取Cookie
 */
public interface CookieManager {

    static <T extends CookieManager> CookieManager getInstance(Class<T> c) throws HttpException {
        try {
            //获取默认构造器
            Constructor<T> constructor = c.getDeclaredConstructor();
            //开启无参构造器访问权限
            constructor.setAccessible(true);
            //实例化
            CookieManager cookieManager = constructor.newInstance();
            //关闭无参构造器访问权限
            constructor.setAccessible(false);
            return cookieManager;
        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            throw new InstanceFailedException(e.getMessage());
        }
    }

    List<HttpCookie> getHttpCookies(String name);

    void setHttpCookies(List<HttpCookie> httpCookies);

    <T> T init(Class<T> c) throws HttpException;
}
